<?php

namespace App\Console\Commands\Feishu;

use App\Models\Common\ConfigModel;
use Illuminate\Console\Command;

class InitFsAppConfigCommand extends Command
{
    protected $signature   = 'feishu:init-fs-app-config';
    protected $description = '初始化飞书应用配置';

    public function handle()
    {
        $value = [];

        $platformNameToOrgId = [
            'callingCards'        => 0,
            'site'                => 0,
            'siteSupplier'        => 0,
            'openShop'            => 0,
            'openSupplier'        => 1,
            'cruise'              => 0,
            'cruiseCheck'         => 0,
            'cruiseCheckSupplier' => 1,
            'cruiseCheckFactory'  => 1,
            'kuafuUi'             => 0,
            'zhaopin'             => 0,
            'supplierChain'       => 1,
            'center'              => 0,
        ];

        // 所有小程序
        foreach (config('feishu.miniProgram') as $platformName => $config) {

            if ($platformName == 'open') {
                continue;
            }

            $value[$config['appId']] = array_merge($config, [
                'orgId'        => $platformNameToOrgId[$platformName],
                'platformName' => $platformName,
            ]);
        }

        // 只有web的应用
        $arr = [
            'supplierChain' => env('EXT_SUPPLIER_FS_APP_ID'),
            'center'        => env('WEB_PLATFORM_CENTER_APPID'),
        ];

        foreach ($arr as $platformName => $appId) {
            $config                  = config('feishu.feishuAppidMap.' . $appId);
            $value[$config['appid']] = [
                'appId'        => $config['appid'],
                'appSecret'    => $config['appSecret'],
                'orgId'        => $platformNameToOrgId[$platformName],
                'platformName' => $platformName,
            ];
        }

        if (config('app.env') == 'dev' || config('app.env') == 'qa') {

            // 供应链
            $value['cli_a4d30dbd48fc5013'] = [
                'appId'        => 'cli_a4d30dbd48fc5013',
                'appSecret'    => 'CdfiZzut6GsEZw4HsLHyUeqFfaNvsFWs',
                'orgId'        => 2,
                'platformName' => 'supplierChain',
            ];

            // 夸父稽核
            $value['cli_a4d61ee359381013'] = [
                'appId'        => 'cli_a4d61ee359381013',
                'appSecret'    => 'oLqXjZBWV7mbO9Yg97OfueYZX76gMnO7',
                'orgId'        => 2,
                'platformName' => 'cruiseCheckSupplier',
            ];

            // 工厂稽核
            $value['cli_a4d66ee4247a5013'] = [
                'appId'        => 'cli_a4d66ee4247a5013',
                'appSecret'    => 'RAdeqzMTlfF9bIdUJe7LQb1Dq1BotHXg',
                'orgId'        => 2,
                'platformName' => 'cruiseCheckFactory',
            ];

        } elseif (config('app.env') == 'pre') {

            // 供应链
            $value['cli_a4edaff2c5fbd013'] = [
                'appId'        => 'cli_a4edaff2c5fbd013',
                'appSecret'    => '551QcsP4fsGdgI97MT2UBbkh1UHjC55K',
                'orgId'        => 2,
                'platformName' => 'supplierChain',
            ];

            // 夸父稽核
            $value['cli_a4ecbe3d903ad00d'] = [
                'appId'        => 'cli_a4ecbe3d903ad00d',
                'appSecret'    => 'TlmhdXk1mX4LL4iUMBs6ng7WJuXxcs7J',
                'orgId'        => 2,
                'platformName' => 'cruiseCheckSupplier',
            ];

            // 工厂稽核
            $value['cli_a4ecbe1346649013'] = [
                'appId'        => 'cli_a4ecbe1346649013',
                'appSecret'    => 'Kni8kNsMDEXy9MWPDNgUqcAwwhbl48vX',
                'orgId'        => 2,
                'platformName' => 'cruiseCheckFactory',
            ];

        } else {

            // 夸父稽核
            $value['cli_a4d66eb7b83a500e'] = [
                'appId'        => 'cli_a4d66eb7b83a500e',
                'appSecret'    => 'Yj2utnForJU6muqoHVjVbbVwDPYMSzFU',
                'orgId'        => 2,
                'platformName' => 'cruiseCheckSupplier',
            ];

            // 工厂稽核
            $value['cli_a4d669a0ca39d00b'] = [
                'appId'        => 'cli_a4d669a0ca39d00b',
                'appSecret'    => 'ZQ7ycBvuroyhITljaHj5dbvmINSDtH7n',
                'orgId'        => 2,
                'platformName' => 'cruiseCheckFactory',
            ];

            // 供应链
            $value['cli_a4d31202ff67900d'] = [
                'appId'        => 'cli_a4d31202ff67900d',
                'appSecret'    => 'h2HCWoQtFbDOR3GVcWnCLhpl32zKfidZ',
                'orgId'        => 2,
                'platformName' => 'supplierChain',
            ];
        }

        // + workOrder
        $value['workOrder'] = [
            'appId'        => config('feishu.center.appID'),
            'appSecret'    => config('feishu.center.AppSecret'),
            'orgId'        => 0,
            'platformName' => 'workOrder',
        ];

        ConfigModel::query()->updateOrCreate([
            'name'   => 'appConfig.'.config('app.env'),
            'module' => 'feishu',
        ], [
            'description' => '飞书应用配置',
            'type'        => 'json',
            'value'       => json_encode($value, JSON_UNESCAPED_UNICODE),
        ]);

    }
}
